package com.asialjim.wechat.official.message.wiki;

import com.asialjim.wechat.database.DataBaseService;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiInfo;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiUrl;
import com.asialjim.wechat.official.message.wiki.vo.MsgWiki;
import com.asialjim.wechat.official.message.wiki.vo.MsgWikiFunctionInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: MsgWikiServiceImpl ]</i></h2><strong>
 * <p> msg wiki service implements
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/25   &nbsp;&nbsp; JDK 8
 */
@Slf4j
@Component
@AllArgsConstructor
public class MsgWikiServiceImpl implements MsgWikiService {
    private final DataBaseService dataBaseService;
    private final Map<String,MsgWikiFunction> functionMap;

    @Override
    public List<MsgWikiFunctionInfo> wikiFunctionInfos() {
        return functionMap.entrySet()
                .stream()
                .map(entry -> MsgWikiFunctionInfo.builder()
                                    .name(entry.getKey())
                                    .cnName(entry.getValue().functionCnName())
                                    .description(entry.getValue().desc())
                                .build())
                .collect(Collectors.toList());
    }

    @Override
    public Boolean add(MsgWiki wiki) {
        if (Objects.isNull(wiki) || Objects.isNull(wiki.getInfo())){
            return Boolean.FALSE;
        }

        MsgWikiInfo info = wiki.getInfo();
        String id = UUID.randomUUID().toString();
        info.setId(id);

        int insertInfo = dataBaseService.insert(info);
        log.info("\r\n\tadd msg wiki info: [{}] result: [{}]",info,insertInfo);

        MsgWikiUrl url = wiki.getUrl();
        if (Objects.nonNull(url)){
            url.setId(id);
            int insertUrl = dataBaseService.insert(url);
            log.info("\r\n\tadd msg wiki url: [{}] result: [{}]",url,insertUrl);
        }

        return insertInfo == 1;
    }

    @Override
    public IPage<MsgWiki> page(Integer size, Integer number) {
        var result = new Page<MsgWiki>();

        Page<MsgWikiInfo> page = new Page<>();
        page.setSize(size);
        page.setCurrent(number);

        IPage<MsgWikiInfo> msgWikiInfoIPage = dataBaseService.selectPage(page, MsgWikiInfo.builder().build());
        log.info("\r\n\tquery wiki info in page: [{}],result: [{}]",page,msgWikiInfoIPage);
        if (Objects.isNull(msgWikiInfoIPage) || msgWikiInfoIPage.getSize() == 0){
            result.setTotal(0);
            result.setRecords(Collections.emptyList());
            result.setCurrent(1);
            result.setSize(number);
            log.info("\r\n\tquery wiki info in page result is empty");
            return result;
        }

        var msgWikis = new ArrayList<MsgWiki>();
        List<MsgWikiInfo> records = msgWikiInfoIPage.getRecords();
        for (MsgWikiInfo record : records) {
            msgWikis.add(MsgWiki.builder()
                            .info(record)
                            .url(dataBaseService.selectById(MsgWikiUrl.class,record.getId()))
                            .build());
        }

        result.setRecords(msgWikis);
        result.setTotal(msgWikiInfoIPage.getTotal());
        result.setCurrent(msgWikiInfoIPage.getCurrent());
        result.setSize(msgWikiInfoIPage.getSize());

        log.info("\r\n\tquery wiki info result:[{}]",result);
        return result;
    }
}
